package com.ztx.service.caiwu.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Repository;

import com.ztx.model.QkInfo;
import com.ztx.model.QkInfoSum;
import com.ztx.model.QkInfoTemp;
import com.ztx.model.QkType;
import com.ztx.model.ReportAllVO;
import com.ztx.pagemodel.SessionInfo;
import com.ztx.pagemodel.easyui.DataGrid;
import com.ztx.pagemodel.easyui.PageHelper;
import com.ztx.service.caiwu.IQkService;
import com.ztx.utils.Constants;
import com.ztx.utils.Utils;

@Repository(IQkService.BEAN_ID)
public class QkServiceImpl implements IQkService {

	@Resource
	private SqlSessionTemplate sqlSessionTemplate;

	@Override
	public DataGrid<QkInfoSum> getqkList(PageHelper ph, QkInfo params) {

		DataGrid<QkInfoSum> datagrid = new DataGrid<>();

		long total = sqlSessionTemplate.selectOne(QkInfoSum.class.getName()
				+ ".qkinfoSumCount", params);
		List<QkInfoSum> list = sqlSessionTemplate.selectList(
				QkInfoSum.class.getName() + ".qkinfoSum", params,
				ph.getRowBounds());
		// 把开始时间和结束时间传回
		for (QkInfoSum qk : list) {
			qk.setStartDateStr(params.getStartDateStr());
			qk.setEndDateStr(params.getEndDateStr());
		}
		datagrid.setTotal(total);
		datagrid.setRows(list);
		return datagrid;
	}

	@Override
	public void doAuditPass(long id, SessionInfo session) throws Exception {
		// 审核通过，更新临时表状态，并把数据存入正式表
		QkInfoTemp temp = sqlSessionTemplate.selectOne(
				QkInfoTemp.class.getName() + ".getQkinfoTempByid", id);
		temp.setAuditStatus(Constants.AUDIT_STATUS_PASS);
		temp.setModifyTime(new Date());
		temp.setModifyUser(session.getUser().getUsername());
		sqlSessionTemplate.update(QkInfoTemp.class.getName() + ".updateById",
				temp);

		QkInfo qkInfo = new QkInfo();
		// 拷贝属性
		BeanUtils.copyProperties(temp, qkInfo);
		qkInfo.setSettleStatus(Constants.SETTLE_STATUS_N);// 结算状态设为未结算
		qkInfo.setCreateTime(new Date());
		qkInfo.setCreateUser(session.getUser().getUsername());
		qkInfo.setModifyTime(null);
		qkInfo.setModifyUser(null);
		sqlSessionTemplate
				.insert(QkInfo.class.getName() + ".addQkInfo", qkInfo);

	}

	@Override
	public void editAndPass(QkInfoTemp info, SessionInfo session)
			throws Exception {
		info.setAuditStatus(Constants.AUDIT_STATUS_PASS);
		info.setModifyTime(new Date());
		info.setModifyUser(session.getUser().getUsername());
		sqlSessionTemplate.update(QkInfoTemp.class.getName() + ".updateById",
				info);

		QkInfo qkInfo = new QkInfo();
		BeanUtils.copyProperties(info, qkInfo);
		qkInfo.setSettleStatus(Constants.SETTLE_STATUS_N);// 结算状态设为未结算
		qkInfo.setCreateTime(new Date());
		qkInfo.setCreateUser(session.getUser().getUsername());
		qkInfo.setModifyTime(null);
		qkInfo.setModifyUser(null);
		sqlSessionTemplate
				.insert(QkInfo.class.getName() + ".addQkInfo", qkInfo);
	}

	@Override
	public void doSynData() throws Exception {
		List<ReportAllVO> listReport = sqlSessionTemplate
				.selectList(ReportAllVO.class.getName() + ".getNotSynData");
		for (ReportAllVO report : listReport) {
			String branchId = report.gettId();
			List<QkInfo> listQkInfo = null;
			switch (branchId) {
			case "yingchuang":
				listQkInfo = yingchuang(report);
				break;
			case "jiahua":
				listQkInfo = jiahua(report);
				break;
			case "bairong":
				listQkInfo = bairong(report);
				break;
			}

			// 保存到欠款信息表
			for (QkInfo qk : listQkInfo) {
				sqlSessionTemplate.insert(
						QkInfo.class.getName() + ".addQkInfo", qk);
			}
		}
		// 更新同步状态为已同步
		sqlSessionTemplate.update(ReportAllVO.class.getName()
				+ ".updateSynStatus");

	}

	/**
	 * 提起盈创的数据
	 * 
	 * @param report
	 * @return
	 */
	private List<QkInfo> yingchuang(ReportAllVO report) {
		List<QkInfo> list = new ArrayList<>();

		// 餐券
		QkInfo canquan = createQkInfo(report);
		canquan.setQkTypeId(Constants.DEBTTYPE_CANQUAN);
		canquan.setQkAmount(report.gettQt());
		list.add(canquan);
		// 微信
		QkInfo weixin = createQkInfo(report);
		weixin.setQkTypeId(Constants.DEBTTYPE_WEIXIN);
		weixin.setQkAmount(report.gettHyk());
		list.add(weixin);

		// 雅酷
		QkInfo yaku = createQkInfo(report);
		yaku.setQkTypeId(Constants.DEBTTYPE_YAKU);
		yaku.setQkAmount(report.gettZp());
		list.add(yaku);

		return list;
	}

	/**
	 * 提取嘉华的数据
	 * @param report
	 * @return
	 */
	private List<QkInfo> jiahua(ReportAllVO report) {
		List<QkInfo> list = new ArrayList<>();
		return list;
	}

	/**
	 * 体
	 * @param report
	 * @return
	 */
	private List<QkInfo> bairong(ReportAllVO report) {
		List<QkInfo> list = new ArrayList<>();
		return list;
	}

	private QkInfo createQkInfo(ReportAllVO report) {
		QkInfo qkInfo = new QkInfo();
		qkInfo.setId(Utils.createId());
		qkInfo.setTradeDate(report.gettCreatedate());
		qkInfo.setBranchId(report.gettId());
		qkInfo.setSettleStatus(Constants.SETTLE_STATUS_N);
		qkInfo.setCreateTime(new Date());
		return qkInfo;
	}

	@Override
	public void settle(QkInfoSum qkinfo, SessionInfo session) throws Exception {
		// 根据欠款类型查询手续费率，并判断金额是否正确
		QkType qktype = sqlSessionTemplate.selectOne(QkType.class.getName()
				+ ".getQkTypeById", qkinfo.getQkTypeId());

		// 财务输入的结算金额
		BigDecimal settleAmount = new BigDecimal(qkinfo.getSettleAmount());
		// 扣除手续费后的金额
		BigDecimal minusRateAmount = null;

		// 物业卡需要扣除水电费和1000元其他费用
		if (!Constants.DEBTTYPE_WUYE.equals(qkinfo.getQkTypeId())) {
			// 扣除手续费后的金额
			minusRateAmount = qkinfo.getSum().subtract(
					qkinfo.getSum().multiply(qktype.getRate()));
		} else {
			// 计算物业卡扣除后的金额
		}
		if (settleAmount.compareTo(minusRateAmount) == 0) {
			// 更新结算状态
			String remark = qkinfo.getStartDateStr() + "结算，结算金额:"
					+ qkinfo.getSettleAmount();
			qkinfo.setRemark(remark);
			sqlSessionTemplate.update(QkInfo.class.getName()
					+ ".updateSettleStatus", qkinfo);
		} else {
			throw new Exception("结算金额和实际金额不相等");
		}
		return;
	}
}
